using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Publicuse.Appication;
using Publicuse.Autofac;
using Publicuse.Entity;
using Publicuse.Entity.AttributeUtil;
using Publicuse.Export;
using Publicuse.Interface;
using Publicuse.Interface.Extend;
using Publicuse.Util;
using Geekeradmin.Authorize;

namespace Geekeradmin.Controllers
{
    /// <summary>
    /// 菜单管理模块
    /// </summary>
    [UserAuthorize("Admin")]
    [Route("api/[controller]")]
    [ApiController]
    public class MenuController : BaseController
    {
        public MenuController(
            IAutofaceInstance autoface,
            IMySqlSugarService mySqlSugar,
            IWebHostEnvironment webHost,
            IOptions<DBConnectionOption> options,
            IBaseServiceExt baseServiceExt
        )
            : base(autoface, mySqlSugar, webHost, options, baseServiceExt) { }

        /// <summary>
        /// 获取菜单树形数据信息
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetTreeList"),AllowAnonymous]
        public async Task<JsonResult> GetTreeList()
        {
            CommonResult result = await MenuApplication
                .GetApplication(this.container)
                .GetTreeList();
            return new JsonResult(result);
        }

        /// <summary>
        /// 获取菜单树形数据信息
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetTreeListb")]
        public async Task<JsonResult> GetTreeListb()
        {
            CommonResult result = await MenuApplication
                .GetApplication(this.container)
                .GetTreeListb();
            return new JsonResult(result);
        }

        /// <summary>
        /// 根据角色编号获取绑定菜单树形数据信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetTreeListc"), AllowAnonymous]
        public async Task<JsonResult> GetTreeListc(string rId)
        {
            CommonResult result = await MenuApplication
                .GetApplication(this.container)
                .GetTreeListc(rId);
            return new JsonResult(result);
        }

        /// <summary>
        /// 根据资源名称获取所有菜单信息
        /// </summary>
        /// <param name="aName"></param>
        /// <returns></returns>
        [HttpGet("GetListb")]
        public async Task<JsonResult> GetListb(string aName)
        {
            CommonResult result = await MenuApplication
                .GetApplication(this.container)
                .GetListb(aName);
            return new JsonResult(result);
        }

        /// <summary>
        /// 绑定用户菜单信息
        /// </summary>
        /// <param name="saveUserRole"></param>
        /// <returns></returns>
        [HttpPost("SaveUserResource")]
        public async Task<JsonResult> SaveUserResource(SaveUserRole saveUserRole)
        {
            CommonResult result = await MenuApplication
                .GetApplication(this.container)
                .SaveUserResource(saveUserRole);
            return new JsonResult(result);
        }

        /// <summary>
        /// 获取菜单详细信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("GetListc")]
        public async Task<JsonResult> GetDetail(int Id)
        {
            CommonResult result = await MenuApplication
                .GetApplication(this.container)
                .GetDetail(Id);
            return new JsonResult(result);
        }

        /// <summary>
        /// 获取菜单信息列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost("GetList")]
        public async Task<JsonResult> GetList(Menu query)
        {
            CommonResult result = await MenuApplication
                .GetApplication(this.container)
                .GetList(query);
            return new JsonResult(result);
        }

        /// <summary>
        /// 根据key获取对应名菜单称
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetMenuAll"), AllowAnonymous]
        public async Task<JsonResult> GetMenuAll()
        {
            CommonResult result = await MenuApplication.GetApplication(this.container).GetMenuAll();
            return new JsonResult(result);
        }

        /// <summary>
        /// 检查资菜单名称是否存在
        /// </summary>
        /// <param name="aName"></param>
        /// <param name="loginUserCode"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("CheckMenuName"), AllowAnonymous]
        public async Task<JsonResult> CheckMenuName(
            string aName,
            string loginUserCode,
            string Id = ""
        )
        {
            CommonResult result = await MenuApplication
                .GetApplication(this.container)
                .CheckMenuName(aName, loginUserCode, Id);
            return new JsonResult(result);
        }

        /// <summary>
        /// 检查菜单编码是否存在
        /// </summary>
        /// <param name="aCode"></param>
        /// <param name="loginUserCode"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("CheckMenuCode"), AllowAnonymous]
        public async Task<JsonResult> CheckMenuCode(
            string aCode,
            string loginUserCode,
            string Id = ""
        )
        {
            CommonResult result = await MenuApplication
                .GetApplication(this.container)
                .CheckMenuCode(aCode, loginUserCode, Id);
            return new JsonResult(result);
        }

        /// <summary>
        /// 新增修改菜单信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost("AddOrUpdate")]
        public async Task<JsonResult> AddOrUpdate(Menu info)
        {
            CommonResult result = await MenuApplication
                .GetApplication(this.container)
                .AddOrUpdate(info, this.webHost);
            return new JsonResult(result);
        }

        /// <summary>
        /// 获取用户绑定角色菜单信息
        /// </summary>
        /// <param name="uId"></param>
        /// <param name="rId"></param>
        /// <param name="loginUserCode"></param>
        /// <returns></returns>
        [HttpGet("GetUserResourceList"), AllowAnonymous]
        public async Task<JsonResult> GetUserResourceList(
            string uId,
            string rId,
            string loginUserCode
        )
        {
            CommonResult result = await MenuApplication
                .GetApplication(this.container)
                .GetUserResourceList(uId, rId, loginUserCode);
            return new JsonResult(result);
        }

        /// <summary>
        /// 获取角色绑定菜单信息
        /// </summary>
        /// <param name="rId"></param>
        /// <param name="loginUserCode"></param>
        /// <returns></returns>
        [HttpGet("GetRoleResourceList"), AllowAnonymous]
        public async Task<JsonResult> GetRoleResourceList(string rId, string loginUserCode)
        {
            CommonResult result = await MenuApplication
                .GetApplication(container)
                .GetRoleResourceList(rId, loginUserCode);
            return new JsonResult(result);
        }

        /// <summary>
        /// 删除菜单信息
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="loginUserCode"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpGet("Delete")]
        public async Task<JsonResult> Delete(string Id, string loginUserCode, string type = "-9")
        {
            CommonResult result = await MenuApplication
                .GetApplication(this.container)
                .Delete(Id, loginUserCode, type);
            return new JsonResult(result);
        }

        /// <summary>
        /// 还原菜单删除信息
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="loginUserCode"></param>
        /// <returns></returns>
        [HttpGet("Reduction"), AllowAnonymous]
        public async Task<JsonResult> Reduction(string Id, string loginUserCode)
        {
            CommonResult result = await MenuApplication
                .GetApplication(this.container)
                .Reduction(Id, loginUserCode);
            return new JsonResult(result);
        }

        /// <summary>
        /// 绑定用户角色菜单信息
        /// </summary>
        /// <param name="uId"></param>
        /// <param name="rId"></param>
        /// <param name="aIds"></param>
        /// <param name="loginUserCode"></param>
        /// <returns></returns>
        [HttpGet("AddResourceToUser"), AllowAnonymous]
        public async Task<JsonResult> AddResourceToUser(
            string uId,
            string rId,
            string aIds,
            string loginUserCode
        )
        {
            CommonResult result = await MenuApplication
                .GetApplication(this.container)
                .AddResourceToUser(uId, rId, aIds, loginUserCode);
            return new JsonResult(result);
        }

        /// <summary>
        /// 绑定角色菜单信息
        /// </summary>
        /// <param name="rId"></param>
        /// <param name="aIds"></param>
        /// <param name="loginUserCode"></param>
        /// <returns></returns>
        [HttpGet("AddResourceToRole"), AllowAnonymous]
        public async Task<JsonResult> AddResourceToRole(
            string rId,
            string aIds,
            string loginUserCode
        )
        {
            CommonResult result = await MenuApplication
                .GetApplication(container)
                .AddResourceToRole(rId, aIds, loginUserCode);
            return new JsonResult(result);
        }

        /// <summary>
        /// 获取用户角色权限菜单
        /// </summary>
        /// <param name="rId"></param>
        /// <param name="loginUserCode"></param>
        /// <returns></returns>
        [HttpGet("GetRoleResource"), AllowAnonymous]
        public async Task<JsonResult> GetRoleResource(string rId, string loginUserCode)
        {
            CommonResult result = await MenuApplication
                .GetApplication(this.container)
                .GetRoleResource(rId, loginUserCode);
            return new JsonResult(result);
        }

        /// <summary>
        /// 导出数据信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet("ExportData")]
        public async Task<IActionResult> ExportData([FromQuery] Menu query)
        {
            var tableName = TableAtributeUtil.GetTable<Menu>();
            return await base.InitExportData(
                this.webHost,
                this.mySqlSugar,
                query,
                "\\template\\report\\Menu.xls",
                "菜单信息统计",
                (a, b, c) =>
                {
                    var ms = MenuExport.ExportData(a, b, c, out var msgb);
                    return ms;
                },
                null,
                tableName
            );
        }
    }
}
